import 'package:enjoy_three/pages/house/components/house_item.dart';
import 'package:enjoy_three/utils/http.dart';
import 'package:enjoy_three/utils/toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  bool isLoading = false;
  List houseList = [];
  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() async {
    setState(() {
      isLoading = true;
    });
    try {
      var res = await http.get('/room');
      if (res['code'] != 10000) return ToastUtil.showError('获取失败');
      ToastUtil.showSuccess('获取成功');
      setState(() {
        houseList = res['data'];
        isLoading = false;
      });
    } catch (e) {
      ToastUtil.showError('请稍后重试');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('房源列表'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),

      body: Container(
        padding: EdgeInsets.all(10),

        child:
            isLoading
                ? Center(child: CircularProgressIndicator())
                : Stack(
                  alignment: AlignmentDirectional.bottomCenter,
                  children: [
                    ListView.separated(
                      itemBuilder: (BuildContext context, int index) {
                        return HouseItem(userInfo: houseList[index]);
                      },
                      separatorBuilder: (BuildContext context, int index) {
                        return Container(height: 10, color: Colors.grey);
                      },
                      itemCount: houseList.length,
                    ),
                    Container(
                      height: 70,
                      width: MediaQuery.of(context).size.width,
                      padding: EdgeInsets.all(10),
                      child: ElevatedButton(
                        onPressed: () {
                          Navigator.pushNamed(context, '/location_list');
                        },
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [Icon(Icons.add), Text('添加房源')],
                        ),
                      ),
                    ),
                  ],
                ),
      ),
    );
  }
}
